package com.ruoyi.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.domain.vo.SysOssVo;
import com.ruoyi.system.mapper.SysOssMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.demo.domain.bo.GoodsBo;
import com.ruoyi.demo.domain.vo.GoodsVo;
import com.ruoyi.demo.domain.Goods;
import com.ruoyi.demo.mapper.GoodsMapper;
import com.ruoyi.demo.service.IGoodsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 商品Service业务层处理
 *
 * @author fjw
 * @date 2023-06-02
 */
@RequiredArgsConstructor
@Service()
public class GoodsServiceImpl implements IGoodsService {

    private final GoodsMapper baseMapper;
    private final SysOssMapper sysOssMapper;

    /**
     * 查询商品
     */
    @Override
    public GoodsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询商品列表
     */
    @Override
    public TableDataInfo<GoodsVo> queryPageList(GoodsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Goods> lqw = buildQueryWrapper(bo);
        Page<GoodsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);

        List<GoodsVo> records = result.getRecords();
        //将里面的image属性 从sys_oss表里面的oss_id 改成url
        for (GoodsVo item :records) {
            String image = item.getImage();
            if(!StringUtils.isEmpty(image)){
                SysOssVo sysOssVo = sysOssMapper.selectVoById(image);
                item.setImage(sysOssVo.getUrl());
            }
        }
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品列表
     */
    @Override
    public List<GoodsVo> queryList(GoodsBo bo) {
        LambdaQueryWrapper<Goods> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Goods> buildQueryWrapper(GoodsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Goods> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), Goods::getName, bo.getName());
        lqw.eq(bo.getTypeId() != null, Goods::getTypeId, bo.getTypeId());
        lqw.like(StringUtils.isNotBlank(bo.getBrand()), Goods::getBrand, bo.getBrand());
        lqw.eq(bo.getAddressId() != null, Goods::getAddressId, bo.getAddressId());
        lqw.eq(StringUtils.isNotBlank(bo.getImage()), Goods::getImage, bo.getImage());
        return lqw;
    }

    /**
     * 新增商品
     */
    @Override
    public Boolean insertByBo(GoodsBo bo) {
        Goods add = BeanUtil.toBean(bo, Goods.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商品
     */
    @Override
    public Boolean updateByBo(GoodsBo bo) {
        Goods update = BeanUtil.toBean(bo, Goods.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Goods entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商品
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
